<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/14
 * Time: 11:26
 */
namespace app\excel\controller;
use think\Controller;
//use think\Upload;
use PHPExcel_IOFactory;
use PHPExcel;

class Index extends Controller
{
    public function index()
    {
        $value = unserialize(urldecode(input('value')));
//        dump($value);
        $save_name = $value['save_name'];
        $temp_name = $value['temp_name'];
        $file_type = $value['file_type'];
        $output = "<b style=\"margin-left: 20px;margin-top: 10px;\">".$temp_name."</b><br>";
        $this->assign([
            'save_name'=>$save_name ? $save_name : '',
//            'temp_name'=>$temp_name ? $temp_name : '',
            'output'=>$temp_name ? $output : '',
            'file_type'=>$file_type ? $file_type : '',
        ]);
        return $this->fetch();
    }

    public function upload()
    {
        //获取表单上传文件
        $file = request()->file('excel-upload');
        if (empty($file)) {
            $this->error('请选择上传文件');
        }
        $temp_name = $file->getInfo('name');
//        var_dump($temp_name);exit();
        $file_types = explode('.',$temp_name);
        $file_type = $file_types [count($file_types) - 1];
        $save_name = date('YmdHis');
        $value = urlencode(serialize(array('temp_name'=>$temp_name,'save_name'=>$save_name,'file_type'=>$file_type)));
        if($file_type == 'xls' || $file_type == 'xlsx' || $file_type == 'csv'){
            //移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads',$save_name);
            if ($info) {
                $this->success('文件上传成功',url('excel/index/index',['value'=>$value]));
//            echo $info->getFilename();
            } else {
                //上传失败获取错误信息
                $this->error($file->getError());
            }
        }else{
            $this->error('上传文件类型错误，请重新上传！');
        }
    }
    public function check(){
        $this->error('检查无误');
    }
}